<?php
/**
 * Protoweb's models base Class
 *
 * This class provides some base functionality in addittion to CodeIgniter
 * and DMZ
 *
 * @author Gonzalez Federico, Viera Leonel
 * @package Protoweb
 * @subpackage model
 */
class Protoweb_models extends DataMapper {

  public function __construct($id = NULL) {
    parent::__construct($id);
  } // end of method __construct($id = NULL)


  /*
   * Devuelve un array asociativo preparado para Smarty, de la forma:
   * array(
   *   key0 => value0
   *   key1 => value1
   *   key2 => value2
   * );
   */
  public function all_to_asoc_array($key, $value) {
    $arr = array();
    foreach($this->get()->all as $row) $arr[$row->$key] = $row->$value;
    return $arr;
  } // end of method all_to_asoc_array($key, $value)

  /*
   * Devuelve un array con todos los valores uno detrás del otro, de la forma:
   * array(0,1,2,3,4,5,..);
   */
  public function all_to_table($fields) {
    $arr = array();
    foreach($this->all as $row) {
      foreach($fields as $field) array_push($arr, $row->$field);
    }
    return $arr;
  } // public function all_to_table($fields)

  /**
   * Devuelve un arreglo con los ids de los objetos relacionados
   *
   * @param string $rel_name
   * @return array
   */
  public function get_relationship_ids($rel_name) {
    $ids = array();

    foreach ($this->$rel_name as $rel) {
      array_push($ids, $rel->id);
    }

    return($ids);
  } // end of method get_relationship_ids($model_name)


  public function update_multiple_relations_with($model_name, $post_array) {
    // get related model
    $related_model = new $model_name();
    $model_name = $related_model->model;

    // obtain old relations
    $old_relations = $this->$model_name->get()->all;

    // delete it
    $this->delete($old_relations);

    // get new relations
    if ($post_array) {
      foreach($post_array as $row_id) {
        $related_model->or_where('id', $row_id);
      }
      return $related_model->get()->all;
    }
    else return $related_model;
  } // end of method update_multiple_relations_with($model_name, $post_array)
  
}// Class protoweb_models

/* End of file protoweb_models.php */
/* Location: ./system/application/models/protoweb_models.php */
